/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

package lime2;

/**
 * The listener within a local strong reaction installed by the Lime system because of an in or rd towards the 
 * federated tuplespace. 
 * <br>
 * There is a slight semantical difference between installing a RD/IN and a (user) weak reaction for the whole
 * federated tuplespace. If there is a matching tuple in the local tuplespace the weak reaction will be fired by a 
 * WeakOp posted by a local strong reaction. This is exactly what we expect from a weak reaction: a non-atomic
 * semantics.
 * In the case of RD/IN, if there is a matching tuple in the local tuplespace, a strong reaction returns it to the
 * agent, wakes it up and uninstall the RD/IN weak reaction (.. which is fired only by "remote" tuples); we preserve
 * "atomic" semantics of RD/IN in the local case.
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

class SystemRemoteBlockingLS extends SystemListener
{
 // private data members	
 private byte type;
 private ReactionID id;
 private LimeTSMgr ltsm;
 private Result agent; 
  
 /**
  * Constructor method.
  * 
  * @param type IN/RD
  * @param id reaction identifier
  * @param ltsm LimeTSMgr of the tuplespace
  * @param agent result on which we synchronize
  */ 
 SystemRemoteBlockingLS(byte type, ReactionID id, LimeTSMgr ltsm, Result agent)
 { 	
  this.type = type;	
  this.id = id;
  this.ltsm = ltsm;
  this.agent = agent;
 } 
  
 /**
  * This method is invoked automatically by the Lime system when the corresponding reaction must fire. 
  * 
  * @param re ReactionEvent
  */ 
 public void reactsToOut(ReactionEvent re)
 {
  // the local strong fired before the local weak: we have to uninstall the local weak 
  ReactionID[] ids = new ReactionID[] {id};	
  ltsm.deregisterWeak(ids); 
   
  // we uninstall also the remote weaks since RD/IN are once reactions (please note that we use priority of events)
  ReactionOp tmp = new ReactionOp(LimeConstants.REMOVE_WEAK, ids, new Result()); 
  CommunicationMgr.get().postEvent(new RemReactionOp(LimeConstants.REM_REMOVE_MULTI, LimeConstants.OPEN, ltsm.getName(), tmp));
   	
  // sets result and releases agent
  switch (type)
  {
   case LimeConstants.IN: 
   	    agent.set(ltsm.doTake(re.getTuple())); // we can use "re" as a template since strong reactions are atomic
   	    agent.lock.release();
   	    break;
   case LimeConstants.RD:
   	    agent.set(re.getTuple()); 
        agent.lock.release(); 
        break;
  }      
 } 	
 
}

